package com.chamber.business.service.impl;

import com.chamber.business.domain.Machine;
import com.chamber.business.domain.MachineInfo;
import com.chamber.business.mapper.MachineMapper;
import com.chamber.business.mqtt.config.MqttFactory;
import com.chamber.business.service.MachineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备管理服务实现类
 */
@Slf4j
@Service
public class MachineServiceImpl implements MachineService {

    @Resource
    private MachineMapper machineMapper;

    @Resource
    private MqttFactory mqttFactory;

    @Override
    public List<Machine> getMachineList() {
        // 查询所有设备列表
        return machineMapper.selectList(null);
    }

    @Override
    public MachineInfo getMachineInfo(Long machineNum) {
        // 这里应该从数据库或缓存中获取设备实时状态
        // 示例实现，实际应用中应该从数据库查询或从缓存获取
        MachineInfo machineInfo = new MachineInfo();
        machineInfo.setMachineNum(machineNum);
        machineInfo.setOxygenConcentration(95L);
        machineInfo.setPressure(101L);
        machineInfo.setAirQuality((short) 1);
        machineInfo.setVolume(50L);
        machineInfo.setIlluminationIntensity(800L);
        machineInfo.setOxygenTherapy(true);

        return machineInfo;
    }

    @Override
    public boolean controlMachine(Long machineNum, Integer status) {
        try {
            // 构建MQTT控制指令
            String topic = "cmd/set";
            String payload = String.format("{\"machineNum\":%d,\"action\":\"power\",\"value\":%d}",
                    machineNum, status);

            // 通过MQTT发送控制指令
            mqttFactory.publishMessage(topic, payload, 1);

            log.info("发送设备控制指令: {}", payload);
            return true;
        } catch (Exception e) {
            log.error("发送设备控制指令失败", e);
            return false;
        }
    }

    @Override
    public boolean adjustMachineParam(Long machineNum, String paramType, Long value) {
        try {
            // 构建MQTT控制指令
            String topic = "cmd/set";
            String payload = String.format("{\"machineNum\":%d,\"action\":\"%s\",\"value\":%d}",
                    machineNum, paramType, value);

            // 通过MQTT发送控制指令
            mqttFactory.publishMessage(topic, payload, 1);

            log.info("发送设备参数调整指令: {}", payload);
            return true;
        } catch (Exception e) {
            log.error("发送设备参数调整指令失败", e);
            return false;
        }
    }

    @Override
    public boolean controlOxygenTherapy(Long machineNum, Boolean enable) {
        try {
            // 构建MQTT控制指令
            String topic = "cmd/set";
            String payload = String.format("{\"machineNum\":%d,\"action\":\"therapy\",\"value\":%b}",
                    machineNum, enable);

            // 通过MQTT发送控制指令
            mqttFactory.publishMessage(topic, payload, 1);

            log.info("发送氧疗功能控制指令: {}", payload);
            return true;
        } catch (Exception e) {
            log.error("发送氧疗功能控制指令失败", e);
            return false;
        }
    }
}